<template>
  <div class="login_bg">
    <el-form :model="ruleForm" :rules="rules" ref="ruleForm" label-width="100px" class="demo-ruleForm">
      <el-form-item>
        <div class="title_style">人才招聘后台登陆</div>
      </el-form-item>

      <el-form-item label="账号" prop="account">
        <el-input placeholder="请输入账号" v-model="ruleForm.account" clearable><template slot="prepend"><i class="el-icon-user-solid"></i></template></el-input>
      </el-form-item>

      <el-form-item label="密码" prop="password">
        <el-input placeholder="请输入密码" v-model="ruleForm.password" type="password" show-password clearable> <template slot="prepend"><i class="el-icon-lock"></i></template></el-input>
      </el-form-item>

      <el-form-item label="角色" prop="roleAid">
        <el-radio-group v-model="ruleForm.roleAid" class="role_style">
          <el-radio :label='"1"'>管理员</el-radio>
          <el-radio :label='"2"'>学生</el-radio>
          <el-radio :label='"3"'>企业</el-radio>
        </el-radio-group>
      </el-form-item>

      <el-form-item>
        <div class="btnstyle">
          <el-button type="primary" @click="submitForm('ruleForm')">登陆</el-button>
          <el-button @click="resetForm('ruleForm')">重置</el-button>
        </div>
      </el-form-item>
    </el-form>
  </div>
</template>

<script>
import { backManageLogin } from "../../api/login";
import {setStore,getStore} from "../../storage/storage";
export default {
  name: "BackLogin",
  data() {
    return {
      ruleForm: {
        account: '',
        password: '',
        roleAid: null,
      },
      rules: {
        account: [
          { required: true, message: '请输入账号', trigger: 'blur' },
        ],
        password: [
          { required: true, message: '请输入密码', trigger: 'blur' },
        ],
        roleAid: [
          { required: true, message: '请选择角色', trigger: 'change' },
        ],
      },
    };
  },
  methods: {
    submitForm(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          const data = {
            account: this.ruleForm.account,
            password: this.ruleForm.password,
            roleAid: this.ruleForm.roleAid,
          }
          backManageLogin(data).then(res=>{
            if (res.data.code === 10000){
              // 登陆成功
              setStore("token",res.data.data.token)
              setStore("account",data.account);
              setStore("password",data.password);
              setStore("roleAid",data.roleAid);
              this.$message({
                duration: 1000,
                message: res.data.message,
                type: "success",
              });
              setTimeout(() => {
                this.$router.push("/backstage/index");
                this.$router.go(0); //刷新
              }, 800);
            }else {
              this.$message({
                duration: 1000,
                message: res.data.message,
                type: "error",
              });
            }
          }).catch(err=>{
            console.log(err)
            this.$message({
              duration: 1000,
              message: "请求异常！",
              type: "error",
            });
          });
        } else {
          console.log('error submit!!');
          return false;
        }
      });
    },
    resetForm(formName) {
      this.$refs[formName].resetFields();
    }
  },
  created() {
    const token = getStore("token");
    // 前端如果存在token
    if (token === null){
      this.ruleForm.account = ''
      this.ruleForm.password = ''
      this.ruleForm.roleAid = ''
    }else {
      // 再次登陆的时候把用户名角色aid回显到form表单
      this.ruleForm.account = getStore("account")
      this.ruleForm.roleAid = getStore("roleAid")
    }
  },
}
</script>

<style scoped>
  .login_bg{
    width: 98vw;
    height: 97vh;
    background: url("https://img-blog.csdnimg.cn/20191013143546617.png") repeat 0 0;
  }
  .header_style{
    text-align: center;
    margin-left: auto;
    margin-right: auto;
    padding-top: 50px;
    width: 570px !important;
    height: auto !important;
    background-color: #d0aeef;
    opacity: 0.7;
  }
  .demo-ruleForm{
    width: 530px;
    margin: 0 auto;
    background-color: #e7dcf6;
    opacity: 0.6;
    padding-right: 40px;
    padding-bottom: 30px;
    position: relative;
    top: 90px;
    border-radius: 8px;
  }
  .demo-ruleForm:hover{
      -webkit-box-shadow: #ccc 0px 10px 10px;
      -moz-box-shadow: #ccc 0px 10px 10px;
      box-shadow: #ccc 0px 10px 10px; 
  }
  .title_style{
    margin-top: 20px;
    margin-left: 90px;
    font-family:'Times New Roman', Times, serif;
    font-size: 20px;
    color: #332a3b;
  }
  .logo{
    background-color: #d0aeef;
    opacity: 0.7;
  }
  .role_style{
    display: flex;
    justify-content: space-around;
    position: relative;
    top: 12px;
  }
  .btnstyle{
    display: flex;
    justify-content: space-between;
  }
</style>